package demo.lisenter;

import jakarta.servlet.ServletContext;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;

@WebListener
public class OnLineLisenter implements HttpSessionListener{
    private int onlineCount = 500; // 用于统计在线人数

    public OnLineLisenter() {
    }

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext context = se.getSession().getServletContext();
        context.setAttribute("onlineCount",onlineCount);
        onlineCount++; // Session对象创建时count变量加1
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        onlineCount--; // session对象销毁时count变量减1
        ServletContext context = se.getSession().getServletContext();
        context.setAttribute("onlineCount", onlineCount);
    }
}
